package com.sywb.chuangyebao.view.dialog;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

public class AvatarSelectDialog extends BaseDialogFragment {

    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_pick)
    TextView tvPick;
    @BindView(R.id.tv_camera)
    TextView tvCamera;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private String title;

    private OnItemListener onItemListener;

    public static AvatarSelectDialog newInstance(Object... pramars) {
        AvatarSelectDialog dialogFragment = new AvatarSelectDialog();
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_avatar_select;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle == null) {
            title = getArguments().getString("p0");
        } else {
            title = bundle.getString("p0");
        }
        tvTitle.setText(title);
    }

    @OnClick({R.id.tv_pick, R.id.tv_camera, R.id.tv_cancel})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pick:
                if (onItemListener != null) {
                    onItemListener.onResult(0, true, "");
                }
                break;
            case R.id.tv_camera:
                if (onItemListener != null) {
                    onItemListener.onResult(1, true, "");
                }
                break;
            case R.id.tv_cancel:
                break;
        }
        exit();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", title);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        setWindowHeight();
    }

    private void setWindowHeight() {
        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        p.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes(p);
    }

    public interface OnItemListener {
        void onResult(int postion, boolean isSuccess, String message);
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }
}
